package com.pureut.storage.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.PickingCompleteDetail;
import com.pureut.storage.dto.NewPickingCompleteDetailDto;
import com.pureut.storage.mapper.PickingCompleteDetailMapper;
import com.pureut.storage.service.PickingCompleteDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: CM
 * @date: 2023/2/1  16:54
 */
@Service
public class PickingCompleteDetailServiceImpl extends ServiceImpl<PickingCompleteDetailMapper, PickingCompleteDetail> implements PickingCompleteDetailService {

    @Resource
    PickingCompleteDetailMapper pickingCompleteDetailMapper;

    @Resource
    RemoteUnitService remoteUnitService;

    /**
     * 返工出库明细列表
     *
     * @param docNum
     * @return
     */
    @Override
    public TableDataInfo<NewPickingCompleteDetailDto> getList(String docNum, PageQuery pageQuery) {
        Page<NewPickingCompleteDetailDto> list = pickingCompleteDetailMapper.getpickingComPleteDetailList(docNum, pageQuery.build());

        //获取单据状态字典数据
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_complete_detail_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));


        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        List<NewPickingCompleteDetailDto> records = list.getRecords();
        for (NewPickingCompleteDetailDto newPickingSaleDetailDto : records) {
            newPickingSaleDetailDto.setWodStatus(newPickingSaleDetailDto.getWodStatus());
            newPickingSaleDetailDto.setWodStatusDict(statusMap.get(newPickingSaleDetailDto.getWodStatus()));

            //单位
            if(newPickingSaleDetailDto.getMaterialUnit() != null){
                newPickingSaleDetailDto.setMaterialUnit(unitByCodeToMap.get(newPickingSaleDetailDto.getMaterialUnit()).getUnitName());
            }

            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + newPickingSaleDetailDto.getTenantId(), String.valueOf(newPickingSaleDetailDto.getMaterialId()));
            newPickingSaleDetailDto.setAuxiliaryAttribute(statsArray);
        }
        return TableDataInfo.build(list);
    }


}
